using System;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PetShop.Web.Controls
{
    public class SimplePager : Repeater
    {
        protected const string HTML1 = "<table cellpadding=0 cellspacing=0><tr><td colspan=2>";
        protected const string HTML2 = "</td></tr><tr class=gridNav><td>";
        protected const string HTML3 = "</td><td align=right>";
        protected const string HTML4 = "</td></tr></table>";

        private const string LINK_PREV =
            "<a href=?page={0}><img src=Images/buttonPrev.gif alt=Previous border=\"0\"></a>";

        private const string LINK_MORE = "<a href=?page={0}><img src=Images/buttonMore.gif alt=More border=\"0\"></a>";
        private const string KEY_PAGE = "page";
        private const string COMMA = "?";
        private const string AMP = "&";
        private static readonly Regex RX = new Regex(@"^&page=\d+", RegexOptions.Compiled);
        private int currentPageIndex;

        private IList dataSource;
        protected string emptyText;
        private int pageSize = 10;

        public override object DataSource
        {
            set
            {
                try
                {
                    dataSource = (IList) value;
                    ItemCount = dataSource.Count;
                }
                catch
                {
                    dataSource = null;
                    ItemCount = 0;
                }
            }
        }

        public int PageSize
        {
            get { return pageSize; }
            set { pageSize = value; }
        }

        protected int PageCount
        {
            get { return (ItemCount - 1)/pageSize; }
        }

        protected virtual int ItemCount { get; set; }

        public virtual int CurrentPageIndex
        {
            get { return currentPageIndex; }
            set { currentPageIndex = value; }
        }

        public string EmptyText
        {
            set { emptyText = value; }
        }

        public void SetPage(int index)
        {
            OnPageIndexChanged(new DataGridPageChangedEventArgs(null, index));
        }

        protected override void OnLoad(EventArgs e)
        {
            if (Visible)
            {
                string page = Context.Request[KEY_PAGE];
                int index = (page != null) ? int.Parse(page) : 0;
                SetPage(index);
            }
        }


        protected override void Render(HtmlTextWriter writer)
        {
            if (ItemCount == 0)
            {
                writer.Write(emptyText);
                return;
            }


            string query = Context.Request.Url.Query.Replace(COMMA, AMP);
            query = RX.Replace(query, string.Empty);


            writer.Write(HTML1);


            base.Render(writer);


            writer.Write(HTML2);


            if (currentPageIndex > 0)
                writer.Write(string.Format(LINK_PREV, (currentPageIndex - 1) + query));


            writer.Write(HTML3);


            if (currentPageIndex < PageCount)
                writer.Write(string.Format(LINK_MORE, (currentPageIndex + 1) + query));


            writer.Write(HTML4);
        }

        protected override void OnDataBinding(EventArgs e)
        {
            int start = CurrentPageIndex*pageSize;
            int size = Math.Min(pageSize, ItemCount - start);

            IList page = new ArrayList();


            for (int i = 0; i < size; i++)
                page.Add(dataSource[start + i]);


            base.DataSource = page;
            base.OnDataBinding(e);
        }

        public event DataGridPageChangedEventHandler PageIndexChanged;

        protected virtual void OnPageIndexChanged(DataGridPageChangedEventArgs e)
        {
            if (PageIndexChanged != null)
                PageIndexChanged(this, e);
        }
    }
}